package cn.edu.xmu.server.listener;

import cn.edu.xmu.server.dao.bo.ChatMsg;
import cn.edu.xmu.server.service.ReceiveService;
import cn.edu.xmu.server.service.SendService;

import cn.edu.xmu.server.util.JacksonUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
@RocketMQMessageListener(consumerGroup = "hza_im_consumer_group",
        topic = "SINGLE_CHAT",
        nameServer = "${rocketmq.name-server:9876}",
        messageModel = MessageModel.CLUSTERING)
public class ReceiverListener implements RocketMQListener<String>{
    private static final Logger logger = LoggerFactory.getLogger(ReceiverListener.class);

    private final ReceiveService receiveService;

    @Autowired
    public ReceiverListener(ReceiveService receiveService) {
        this.receiveService = receiveService;
    }

    @Override
    public void onMessage(String msg) {
        logger.info("body={}", msg);
        ChatMsg chatMsg = JacksonUtil.toObj(msg, ChatMsg.class);
        this.receiveService.handleMsg(chatMsg);
    }
}